// pages/todo/todo.js

const http = require("./../../utils/http.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: [],
    select: 1,
    ids: [],
    flags: [],
    content:[],
    unfinishContent: [],
    unfinishTheme: [],
    finishContent: [],
    finishTheme: [],
    listTask:[]
  },

  /**
   * 改变复选框的状态
   */
  onChange(event){
    var index = event.currentTarget.dataset.index
    var checks = this.data.checked
    checks[index] = !checks[index]
    this.setData({
      checked: checks
    })
  },
  
  selectAll(){
    var checks = this.data.checked;
    var flag = 0;
    for(var i = 0; i < checks.length; i++){
      if(checks[i] == false) {
        flag = 1;
        break;
      }
    }
    if(flag == 1) {
      for(var i = 0; i < checks.length; i++){
        checks[i] = true;
      }
    } else{
      for(var i = 0; i < checks.length; i++){
        checks[i] = false;
      }
    }
    this.setData({
      checked: checks
    })
  },

  editTask(event){
    var that = this
    var index = event.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/addTask/addTask?id=' + that.data.ids[index] +'&content=' + that.data.unfinishContent[index] + '&theme=' + that.data.unfinishTheme[index] + '&classfy=put'
    })
  },

  updateBatch(event){
    var tasks = [];
    var indexs = [];
    var data = this.data;
    console.log(data)
    for(var i = 0; i < data.checked.length; i++) {
      if(data.checked[i] == true) {
        indexs.push(i)
      }
    }
    console.log(indexs)
    for(var i = 0; i < indexs.length; i++) {
      var task = new Object();
      task.id = data.ids[indexs[i]];
      task.theme = data.unfinishTheme[indexs[i]];
      task.flag = 1;
      task.content = data.unfinishContent[indexs[i]];
      task.userId = 'Taylor';
      task.time = new Date().toISOString().substring(0, 10);
      tasks.push(task)
    }
    http.put('http://127.0.0.1/tasks/updateBatch', JSON.stringify(tasks),
    res => {
      this.listUnFinishTask();
    })
  },

  deleteBatch(event) {
    var ids = [];
    var indexs = [];
    var data = this.data;
    console.log(data)
    for(var i = 0; i < data.checked.length; i++) {
      if(data.checked[i] == true) {
        indexs.push(i)
      }
    }
    for(var i = 0; i < indexs.length; i++) {
      ids.push(data.ids[indexs[i]].toString())
    }
    http.Delete('http://127.0.0.1/tasks/deleteBatch', ids,
    res => {
      this.listFinishTask();
    })
  },

  listFinishTask(){
    console.log(wx.getStorageSync('username'))
    http.get('http://127.0.0.1/tasks/finish', {
      "username": wx.getStorageSync('username')
    }, res => {
      console.log(res)
      var arrIds = new Array();
      var arrContent = new Array();
      var arrTheme = new Array();
      var arrFlags = new Array();
      var checks = new Array();
      for (var i = 0; i < res.data.data.length; i++) { 
        arrIds[i] = res.data.data[i]['id'];
        arrFlags[i] = !res.data.data[i]['flag'];
        arrContent[i] = res.data.data[i]['content'];
        arrTheme[i] = res.data.data[i]['theme'];
        checks[i] = false;
      }
      if(res.data.flag == true){
        this.setData({ 
          ids: arrIds,
          flags: arrFlags,
          finishContent: arrContent,
          finishTheme: arrTheme,
          checked: checks
        });
        this.setData({
          content: this.data.finishContent
        })
      } else {
        this.setData({ 
          ids: [],
          flags: [],
          finishContent: [],
          finishTheme: [],
          checked: []
        });
        this.setData({
          content: []
        })
      }
    });
  },

  listUnFinishTask(){
    http.get('http://127.0.0.1/tasks/unfinish', {
      "username": wx.getStorageSync('username')
    }, res => {
      var arrIds = new Array();
      var arrContent = new Array();
      var arrTheme = new Array();
      var arrFlags = new Array();
      var checks = new Array();
      for (var i = 0; i < res.data.data.length; i++) { 
        arrIds[i] = res.data.data[i]['id'];
        arrFlags[i] = res.data.data[i]['flag'];
        arrContent[i] = res.data.data[i]['content'];
        arrTheme[i] = res.data.data[i]['theme'];
        checks[i] = false;
      }
      if(res.data.flag == true){
        this.setData({ 
          ids: arrIds,
          flags: arrFlags,
          unfinishContent: arrContent,
          unfinishTheme: arrTheme,
          checked: checks
        });
        this.setData({
          content: this.data.unfinishContent
        })
      } else {
        this.setData({ 
          ids: [],
          flags: [],
          unfinishContent: [],
          unfinishTheme: [],
          checked: []
        });
        this.setData({
          content: []
        })
      }
    });
  },

  delTask(event){
    var that = this
    var index = event.currentTarget.dataset.index
    console.log(index)
    console.log(this.data.unfinishTheme[index])
    http.Delete('http://127.0.0.1/tasks/' + this.data.ids[index], {
    }, res => {
      if(res.data.flag == true){
        this.listTask();
        wx.showToast({
          title: '删除成功',
          duration: 2000
        })
      } else{
        wx.showToast({
          title: '删除失败',
          duration: 2000
        })
      }
    })
  },

  /**
   * 获取具体类型的贴子
   */
  selected(e) {
    this.setData({
      select: e.target.dataset.type,
    })
  },

    /** 
   * 进入发表页面
   */
  addTask() {
    wx.navigateTo({
      url: '/pages/addTask/addTask'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.listUnFinishTask();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})